<template>
  <div class="divBox">
    <el-card class="box-card">
      <div slot="header" class="clearfix">
        <div class="container">
          <el-form size="small" label-width="100px" :inline="true">
            <el-form-item label="禁用状态：">
              <el-select
                v-model="tableFrom.status"
                clearable
                placeholder="请选择"
                class="selWidth"
                @change="getList()"
              >
                <el-option label="是" value="1" />
                <el-option label="否" value="0" />
              </el-select>
            </el-form-item>
            <el-form-item label="关键字：">
              <el-input
                v-model="tableFrom.keywords"
                @keyup.enter.native="getList()"
                placeholder="请输入账号、名称或关键字"
                class="selWidth"
              >
                <el-button
                  slot="append"
                  icon="el-icon-search"
                  class="el-button-solt"
                  @click="getList()"
                />
              </el-input>
            </el-form-item>
          </el-form>
        </div>
        <el-button size="small" type="primary" @click="onAdd"
          >添加管理员</el-button
        >
      </div>
      <el-table
        v-loading="listLoading"
        :data="tableData.data"
        style="width: 100%"
        size="small"
        border
        stripe
      >
        <el-table-column prop="adminId" label="ID" min-width="80" />
        <el-table-column prop="level" label="用户等级" min-width="100">
          <template v-slot="scope">
            <span v-if="scope.row.level === 1">总管理</span>
            <span v-else>地方管理</span>
          </template>
        </el-table-column>
        <el-table-column prop="realName" label="管理员姓名" min-width="100" />
        <el-table-column prop="roleNames" label="身份" min-width="180" />
        <el-table-column prop="cityNames" label="城市" min-width="180" />
        <el-table-column prop="account" label="账号" min-width="100" />
        <el-table-column prop="status" label="是否启用" min-width="65">
          <template v-slot="scope">
            <el-switch
              v-model="scope.row.status"
              active-color="#13ce66"
              :active-value="1"
              :inactive-value="0"
              @change="onchangeIsShow(scope.row)"
            />
          </template>
        </el-table-column>
        <el-table-column prop="createTime" label="创建时间" min-width="130" />
        <el-table-column label="操作" min-width="250" fixed="right">
          <template v-slot="scope">
            <el-button
              icon="el-icon-edit"
              type="primary"
              size="small"
              @click="onEdit(scope.row.adminId)"
            >
            </el-button>
            <el-button
              type="warning"
              size="small"
              @click="onEditRole(scope.row.adminId)"
            >
              分配角色
            </el-button>
            <el-button
              type="danger"
              size="small"
              @click="onEditCity(scope.row.adminId)"
            >
              分配城市
            </el-button>
          </template>
        </el-table-column>
      </el-table>
      <div class="block">
        <el-pagination
          :page-sizes="[5, 10, 15, 20]"
          :page-size="tableFrom.limit"
          :current-page="tableFrom.page"
          layout="total, sizes, prev, pager, next, jumper"
          :total="tableData.total"
          @size-change="handleSizeChange"
          @current-change="pageChange"
        />
      </div>
    </el-card>
    <el-dialog
      title="添加管理员"
      :visible.sync="dialogVisible"
      width="50%"
      @close="addRoleDialogClosed"
    >
      <el-form
        :model="addAdminFrom"
        :rules="FormRules"
        ref="addFromRef"
        label-width="80px"
      >
        <el-form-item label="名称 :" prop="realName">
          <el-input
            v-model="addAdminFrom.realName"
            placeholder="请输入名称"
          ></el-input>
        </el-form-item>
        <el-form-item label="电话 :" prop="phone">
          <el-input
            v-model="addAdminFrom.phone"
            placeholder="请输入电话"
          ></el-input>
        </el-form-item>
        <el-form-item label="密码 :" prop="pwd">
          <el-input
            v-model="addAdminFrom.pwd"
            placeholder="请输入密码"
          ></el-input>
        </el-form-item>
        <el-form-item label="账号 :" prop="account">
          <el-input
            v-model="addAdminFrom.account"
            placeholder="请输入账号"
          ></el-input>
        </el-form-item>
        <el-form-item label="用户等级 :" prop="level">
          <el-select
            v-model="addAdminFrom.level"
            placeholder="请选择"
            clearable
          >
            <el-option
              v-for="item in options"
              :key="item.value"
              :label="item.label"
              :value="item.value"
            >
            </el-option>
          </el-select>
        </el-form-item>
      </el-form>
      <span slot="footer" class="dialog-footer">
        <el-button @click="dialogVisible = false">取 消</el-button>
        <el-button type="primary" @click="addAdmin">确 定</el-button>
      </span>
    </el-dialog>
    <el-dialog
      title="修改管理员信息"
      :visible.sync="editDialogVisible"
      width="50%"
      @close="editRoleDialogClosed"
    >
      <el-form
        :model="editAdminFrom"
        :rules="FormRules"
        ref="editFromRef"
        label-width="80px"
      >
        <el-form-item label="名称 :" prop="realName">
          <el-input
            v-model="editAdminFrom.realName"
            placeholder="请输入名称"
          ></el-input>
        </el-form-item>
        <el-form-item label="电话 :" prop="phone">
          <el-input
            v-model="editAdminFrom.phone"
            placeholder="请输入电话"
          ></el-input>
        </el-form-item>
        <el-form-item label="密码 :">
          <el-input
            v-model="editAdminFrom.newPassword"
            placeholder="请输入新密码"
          ></el-input>
        </el-form-item>
      </el-form>
      <span slot="footer" class="dialog-footer">
        <el-button @click="editDialogVisible = false">取 消</el-button>
        <el-button type="primary" @click="editData">确 定</el-button>
      </span>
    </el-dialog>

    <!-- 分配角色 -->
    <el-dialog
      title="分配角色"
      :visible.sync="editRoleDialogVisible"
      width="40%"
    >
      <el-checkbox-group v-model="checkedRoles" class="checkbox">
        <el-checkbox
          class="checkbox1"
          v-for="item in roles"
          :label="item.roleId"
          :key="item.roleId"
          >{{ item.roleName }}</el-checkbox
        >
      </el-checkbox-group>
      <span slot="footer" class="dialog-footer">
        <el-button @click="editRoleDialogVisible = false">取 消</el-button>
        <el-button type="primary" @click="editRoleData">确 定</el-button>
      </span>
    </el-dialog>
    <el-dialog
      title="分配城市"
      :visible.sync="editCityDialogVisible"
      width="40%"
    >
      <el-checkbox-group v-model="checkList" class="checkbox">
        <el-checkbox
          class="checkbox1"
          v-for="item in citys"
          :label="item.id"
          :key="item.id"
          >{{ item.name }}</el-checkbox
        >
      </el-checkbox-group>
      <span slot="footer" class="dialog-footer">
        <el-button @click="editCityDialogVisible = false">取 消</el-button>
        <el-button type="primary" @click="editCityData">确 定</el-button>
      </span>
    </el-dialog>
  </div>
</template>

<script>
import {
  menuRoleApi,
  adminListApi,
  adminCreateApi,
  getInfoByIdApi,
  adminUpdateApi,
  adminStatusApi,
  getAdminRoleApi,
  getAdminRoleUpdateApi,
  getAdminCityApi,
  getAdminCityUpdateApi,
} from "@/api/setting";
import { getCityList } from "@/api/system";
import { fromList } from "@/libs/constants.js";
export default {
  name: "SystemRole",

  data() {
    return {
      checkedRolesId: "",
      // 添加用户的表单区域
      addAdminFrom: {
        realName: "",
        phone: "",
        pwd: "",
        account: "",
        level: "",
      },
      tableCityFrom: {
        page: 1,
        limit: 1000000000,
        keywords: "",
        status: "",
        date: "",
      },
      //编辑用户的表单区域
      editAdminFrom: {
        realName: "",
        phone: "",
        newPassword: "",
        adminId: "",
      },
      editRoleAdminFrom: "",
      editCityAdminFrom: {},

      fromList: fromList,
      grid: {
        xl: 7,
        lg: 7,
        md: 12,
        sm: 24,
        xs: 24,
      },
      password: {
        oldPassword: "",
        newPassword: "",
      },
      options: [
        {
          value: "1",
          label: "总管理",
        },
        {
          value: "2",
          label: "地方管理",
        },
      ],
      value: "",
      // 管理员获取列表
      tableData: {
        data: [],
        total: 0,
      },
      listLoading: true,
      tableFrom: {
        page: 1,
        limit: 5,
        keywords: "",
        status: "",
        date: "",
      },
      timeVal: [],
      dialogVisible: false,
      editDialogVisible: false,
      editRoleDialogVisible: false,
      editCityDialogVisible: false,
      FormRules: {
        account: [
          { required: true, message: "请输入账号", trigger: "blur" },
          {
            min: 5,
            max: 12,
            message: "长度在 5 到 12 个字符",
            trigger: "blur",
          },
        ],
        phone: [
          { required: true, message: "请输入电话", trigger: "blur" },
          {
            min: 5,
            max: 12,
            message: "长度在 5 到 12 个字符",
            trigger: "blur",
          },
        ],
        pwd: [
          { required: true, message: "请输入密码", trigger: "blur" },
          {
            min: 5,
            max: 12,
            message: "长度在 5 到 12 个字符",
            trigger: "blur",
          },
        ],
        realName: [
          { required: true, message: "请输入名称", trigger: "blur" },
          {
            min: 5,
            max: 12,
            message: "长度在 5 到 12 个字符",
            trigger: "blur",
          },
        ],
      },
      FormRoleRules: {},
      FormCityRules: {},
      // 分配角色
      checkedRoles: [],
      roles: {},
      citys: {},
      checkList: [],
      checkedCityId: "",
    };
  },
  mounted() {
    this.getList();
    this.getRole();
    this.getCity();
  },
  methods: {
    // 获取列表
    getList(num) {
      this.listLoading = true;
      this.tableFrom.page = num ? num : this.tableFrom.page;
      adminListApi(this.tableFrom)
        .then((res) => {
          this.tableData.data = res.data.list;
          this.tableData.total = res.data.total;
          this.listLoading = false;
        })
        .catch((res) => {
          this.tableData.data = [];
          this.tableData.total = 0;
          this.listLoading = false;
          this.$message.error(res.message);
        });
    },
    // 获取角色列表
    getRole() {
      menuRoleApi()
        .then((res) => {
          this.roles = res.data;
        })
        .catch(() => {
          this.$message.error("获取失败");
        });
    },
    getCity() {
      getCityList(this.tableCityFrom)
        .then((res) => {
          this.citys = res.data.list;
        })
        .catch(() => {
          this.$message.error("获取失败");
        });
    },

    pageChange(page) {
      this.tableFrom.page = page;
      this.getList();
    },
    handleSizeChange(val) {
      this.tableFrom.limit = val;
      this.getList();
    },
    // 管理员状态切换
    onchangeIsShow(row) {
      adminStatusApi(row.adminId, row.status)
        .then(({ message }) => {
          this.$message.success(message);
        })
        .catch(({ message }) => {
          this.$message.error(message);
        });
    },
    // 添加管理员按钮
    onAdd() {
      this.dialogVisible = true;
    },
    // 添加管理员对话框关闭事件
    addRoleDialogClosed() {
      // console.log(this.addAdminFrom);
      this.$refs.addFromRef.resetFields();
    },
    // 修改管理员密码对话框关闭事件
    editRoleDialogClosed() {
      this.$refs.editFromRef.resetFields();
    },
    // 添加管理员
    addAdmin() {
      this.$refs.addFromRef.validate((valid) => {
        if (!valid) return;
        adminCreateApi(this.addAdminFrom)
          .then(() => {
            this.getList();
            this.$message.success("添加管理员成功！");
            this.dialogVisible = false;
          })
          .catch(() => {
            this.$message.error("添加管理员失败！");
            this.dialogVisible = false;
          });
      });
    },
    // 点击编辑按钮触发事件
    onEdit(id) {
      this.editDialogVisible = true;
      getInfoByIdApi(id)
        .then((res) => {
          this.editAdminFrom.realName = res.data.realName;
          this.editAdminFrom.phone = res.data.phone;
          this.editAdminFrom.newPassword = res.data.pwd;
          this.editAdminFrom.adminId = res.data.adminId;
        })
        .catch((message) => {
          this.$message.error(message);
        });
    },
    // 编辑对话框确认按钮点击事件
    editData() {
      console.log(this.editAdminFrom);
      adminUpdateApi(this.editAdminFrom.adminId, this.editAdminFrom)
        .then((res) => {
          this.editDialogVisible = false;
          this.$message.success("管理员修改成功");
        })
        .catch((res) => {
          this.editDialogVisible = false;
          this.$message.error("管理员修改失败！");
        });
    },
    // 分配角色编辑按钮
    onEditRole(id) {
      this.editRoleDialogVisible = true;
      this.checkedRoles = [];
      getAdminRoleApi(id)
        .then((res) => {
          this.checkedRolesId = id;
          for (var i = 0; i < res.data.length; i++) {
            this.checkedRoles[i] = res.data[i].roleId;
          }
          this.checkedRoles = Array.from(this.checkedRoles);
        })
        .catch(() => {
          this.$message.error("获取失败！");
        });
    },
    // 角色分配对话框确认按钮
    editRoleData() {
      console.log(this.checkedRoles);
      getAdminRoleUpdateApi(this.checkedRolesId, this.checkedRoles)
        .then(() => {
          this.editRoleDialogVisible = false;
          this.$message.success("操作成功！");
          this.getList();
        })
        .catch(() => {
          this.editRoleDialogVisible = false;
          this.$message.error("操作失败！");
        });
    },
    onEditCity(id) {
      this.editCityDialogVisible = true;
      this.checkList = [];
      getAdminCityApi(id)
        .then((res) => {
          this.checkedCityId = id;
          for (var i = 0; i < res.data.length; i++) {
            this.checkList[i] = res.data[i].id;
          }
          this.checkList = Array.from(this.checkList);
          console.log(this.checkList);
        })
        .catch(() => {
          this.$message.error("获取失败！");
        });
    },
    editCityData() {
      getAdminCityUpdateApi(this.checkedCityId, this.checkList).then((res) => {
        this.editCityDialogVisible = false;
        this.$message.success("操作成功！");
        this.getList();
      });
    },
  },
};
</script>

<style scoped lang="scss">
.el-switch {
  width: 40px;
}
.checkbox {
  .checkbox1 {
    width: 100px;
    height: 50px;
  }
}
</style>
